package com.iims.market.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.exception.CustomException;
import com.iims.common.utils.SeqNumUtils;
import com.iims.market.vo.*;
import com.iims.market.vo.GroupContractSearchQuery;
import com.iims.market.domain.Contract;
import com.iims.market.mapper.ContractMapper;
import com.iims.market.service.IContractCommonService;
import com.iims.market.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合同服务
 *
 * @author fanpei
 * @date 2021/6/17 15:09
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {

    @Autowired
    private IContractCommonService commonService;

    @Override
    public List<ContactRowVo> selectContractList(Contract contract) {
        List list = baseMapper.selectContractList(contract);
        return SeqNumUtils.initSeqNum(contract, list);
    }

    @Override
    public ContractStats selectContractStat(Contract contract) {
        return baseMapper.selectContractStats(contract);
    }

    @Override
    public List<ContactRowVo> selectContractListByGroup(GroupContractSearchQuery groupSearchQuery) {
        groupSearchQuery.generateSqlArgs();
        List list = baseMapper.selectContractListByGroup(groupSearchQuery);
        return SeqNumUtils.initSeqNum(groupSearchQuery, list);
    }

    @Override
    public ContractStats selectContractStatsByGroup(GroupContractSearchQuery groupSearchQuery) {
        groupSearchQuery.generateSqlArgs();
        return baseMapper.selectContractStatsByGroup(groupSearchQuery);
    }

    @Override
    public ContactRowVo selectDetailById(Long contractId) {
        ContactRowVo contract = baseMapper.selectContractDetailById(contractId);
        if (Validator.isNotNull(contract)) {
            String extenions = baseMapper.selectContractExtentions(contractId);
            if (Validator.isNotEmpty(extenions)) {
                contract.setContractReqInfo(JSONUtil.toBean(extenions, ContractReqInfoVo.class));
            }
        }
        return contract;
    }

    @Override
    public int insertContract(Contract contract) {
        commonService.checkNameAndCodeUnique(
                contract.getContractId(), contract.getContractName(), contract.getContractCode());
        //转换为json
        contract.setExtentions(JSONUtil.toJsonStr(contract.getContractReqInfo()));
        //初始化
        contract.setBigStatus(0);
        //contract.setCollectStatus("0");
        //contract.setDeliveryStatus("0");
        contract.setFileStatus(0);
        //contract.setInvoiceStatus("0");
        //contract.setTakingsConfirmStatus("0");
        return baseMapper.insert(contract);
    }

    @Override
    public int updateContract(Contract contract) {
        commonService.checkNameAndCodeUnique(
                contract.getContractId(), contract.getContractName(), contract.getContractCode());
        //转换为json
        contract.setExtentions(JSONUtil.toJsonStr(contract.getContractReqInfo()));
        return baseMapper.updateById(contract);
    }


    @Override
    public int deleteContractById(Long contractId) {
        QueryWrapper queryWrapper = new QueryWrapper<Contract>()
                .select("big_status")
                .eq("contract_id", contractId);
        Contract contract = baseMapper.selectOne(queryWrapper);
        if (Validator.isNotEmpty(contract)) {
            if (!"0".equals(contract.getBigStatus())) {
                throw new CustomException("合同id" + contractId + "的合同已进入流程处理，无法删除");
            }
            return baseMapper.deleteById(contractId);
        }
        return 1;
    }

    @Override
    public int fillingContract(Contract contract) {
        LambdaUpdateWrapper<Contract> updateWrapper = new LambdaUpdateWrapper<>();
        String filingStatus = "0";
        if (Validator.isNotEmpty(contract.getFileLocation()) &&
                Validator.isNotEmpty(contract.getFileCode())) {
            filingStatus = "1";//已归档
        }
        updateWrapper.eq(Contract::getContractId, contract.getContractId())
                .set(Contract::getFileCode, contract.getFileCode())
                .set(Contract::getFileLocation, contract.getFileLocation())
                .set(Contract::getFileStatus, filingStatus);

        return baseMapper.update(null, updateWrapper);
    }

    @Override
    public ContractContactorVo selectContractContator(Long contractId) {
        return baseMapper.selectContractContator(contractId);
    }
}
